package com.mokairui.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Description 线程池异常处理
 * @Author Mokairui
 * @Since 2021/10/6
 */
@Slf4j(topic = "c.exceptionExecute")
public class ThreadPoolExceptionExecute {
    /**
     * 两种处理方式
     *  1. 在线程run中进行surround catch捕获异常
     *  2. 不适用 runnable 使用 callable 因为其异常会被封装到 future 中
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(1);

        Future<Boolean> result = threadPool.submit(() -> {
            log.debug("running...");
            int i = 1 / 0;
            return false;
        });

        log.debug("{}", result.get());
    }
}
